#! /bin/sh

case "$1" in
*.*)	fullemojis="$1"; shift;;
"")	echo "Usage: `basename $0` .../full-emoji-list.html [emoji style]..." >&2
	echo >&2
	echo "This script extracts emojis graphics (.png format) from a downloaded copy of" >&2
	echo "	http://www.unicode.org/emoji/charts/full-emoji-list.html" >&2
	echo "for the selected emoji style sets, or (if none selected) all of them:" >&2
	echo "	apple google twitter emojione facebook samsung windows" >&2
	echo "and always extracts common emoji graphics." >&2
	echo >&2
	echo "Warning: with all styles selected, this may take a few hours on Cygwin." >&2
	exit;;
*)	echo missing file name of full emoji list >&2; exit;;
esac

sel=
while	case "$1" in
	apple)	sel="1$sel";;
	google)	sel="2$sel";;
	twitter)	sel="3$sel";;
	emojione)	sel="4$sel";;
	facebook)	sel="5$sel";;
	samsung)	sel="6$sel";;
	windows)	sel="7$sel";;
	"")	false;;
	*)	echo emoji set "$1" not known; exit;;
	esac
do	shift
done

sel=0${sel:-1234567}
export sel

echo "Warning: this may take a few hours on Cygwin" >&2

LC_ALL=C
export LC_ALL

total=`grep -e "name='\([^']*\)'.*U+" "$fullemojis" | wc -l`
export total

(
echo "Extracting $total emojis from $fullemojis" >&2

cat <<\/EOS
n=0

name () {
  ename=$1
  style=0
  n=$(( $n + 1 ))
  p=$(( ${n}00 / $total ))
  echo "emoji $ename (${p}%)" >&2
}

img0 () {
  echo " common 0/$ename.png" >&2
  echo "$1" | base64 -d > 0/$ename.png
}

img () {
  style=$(( $style + 1 ))
  case $sel in
  *$style*)	echo "$1" | base64 -d > $style/$ename.png;;
  esac
}

imgskip () {
  style=$(( $style + 1 ))
  case $sel in
  *$style*)	echo " skip $style/$ename.png" >&2;;
  esac
}

/EOS

mkdirlink () {
  case $sel in
  *$1*)	mkdir -p $1
  	[ -e "$2" ] || ln -s "$1" "$2"
  	;;
  esac
}
mkdirlink 1 apple
mkdirlink 2 google
mkdirlink 3 twitter
mkdirlink 4 emojione
mkdirlink 5 facebook
mkdirlink 6 samsung
mkdirlink 7 windows
mkdirlink 0 common

cat "$fullemojis" |
sed -e "s/^.*name='\([^']*\)'.*U+.*/name \1/" -e "t name" \
    -e "s/.*—.*/imgskip/" -e t \
    -e "s@^.*….*src='data:image/png;base64,\([^']*\)'.*@img0 \1@" -e t \
    -e "s@^.*src='data:image/png;base64,\([^']*\)'.*@img \1@" -e t \
    -e d \
    -e ": name" \
    -e "s,_,-,g"
) | sh
